//
//  ChannelListGridViewCell.swift
//  GridViewExample
//
//  Created by Kyohei Ito on 2017/05/11.
//  Copyright © 2017年 Kyohei Ito. All rights reserved.
//

import GridView

typealias callbackfunc=(_ selectIndex:Int,_ width :CGFloat)->Void

class ChannelListGridViewCell: GridViewCell {
    @IBOutlet weak var channelLabel: UILabel!
    
    @IBOutlet weak var rightView: UIImageView!
    static var nib: UINib {
        return UINib(nibName: "ChannelListGridViewCell", bundle: Bundle(for: self))
    }
    var col = 0
    var moveCallBack : callbackfunc!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        channelLabel.font = .boldSystemFont(ofSize: 16)
        channelLabel.textColor = .darkGray
        channelLabel.textAlignment = .left
        
        let panGR : UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(panGRAct as (UIPanGestureRecognizer) -> ()))
        self.rightView.addGestureRecognizer(panGR)
        self.rightView.isUserInteractionEnabled = true
        
    }
    
    func panGRAct(rec : UIPanGestureRecognizer){
        let point = rec.translation(in: self.rightView)
//        CGPoint point = [rec translationInView:self.view];
        NSLog("%f,%f",point.x,point.y);
        self.frame = CGRect.init(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width + point.x, height: self.frame.size.height)
        rec.setTranslation(CGPoint.init(x: 0.0,y: 0.0), in: self.rightView)
        if (rec.state == UIGestureRecognizerState.ended || rec.state == UIGestureRecognizerState.cancelled){
            self.moveCallBack(self.col,self.frame.size.width)
        }
    }
    
    
    func configure(_ channelName: String,id :Int) {
        channelLabel.text = channelName
        self.col = id
    }
}
